package UserInterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import BusinessLayer.Facade;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;


/**
 *panel correspondant à la notation des étudiants 
 * @author maxou
 */
public class PanelNotation extends javax.swing.JPanel implements ActionListener{

    private GUINotation guiNotation;
	/** Creates 
     * @param guiNotation 
     * 
     **/
    public PanelNotation(GUINotation guiNotation) {
    	this.guiNotation=guiNotation;
        
    	initComponents();
        Facade.getInstance().initDataScolarite();
        this.JButtonDepartement.addActionListener(this);
        this.JButtonReload.addActionListener(this);
        
        ensDep=Facade.getInstance().getNomsEnsDepartements();  


	        for (int i=0 ; i < ensDep.size(); i++ ) {
	           this.JButtonDepartement.addItem(this.ensDep.get(i).toString());
	        }
        
    }


    /**
     * initialisation des composants
     */
    private void initComponents() {

    	/**
    	 * déclaration des boutons et label
    	 */
    	jLabelDepartement = new javax.swing.JLabel();
        jLabelAnnee = new javax.swing.JLabel();
        jLabelSemestre = new javax.swing.JLabel();
        jLabelUe = new javax.swing.JLabel();
        JButtonDepartement = new javax.swing.JComboBox();
        JButtonAnnee = new javax.swing.JComboBox();
        JButtonSemestre = new javax.swing.JComboBox();
        JButtonUE = new javax.swing.JComboBox();
        jLabelEcue = new javax.swing.JLabel();
        JButtonEcue = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        JButtonReload = new javax.swing.JButton();

        jLabelDepartement.setText("Departement");

        jLabelAnnee.setText("Annee");

        jLabelSemestre.setText("Semestre");

        jLabelUe.setText("UE");

        JButtonDepartement.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selectionner" }));
        JButtonDepartement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JButtonDepartementActionPerformed(evt);
            }
        });

        JButtonAnnee.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Annee" }));

        JButtonSemestre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Semestre" }));
        JButtonSemestre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JButtonSemestreActionPerformed(evt);
            }
        });

        JButtonUE.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ue" }));

        jLabelEcue.setText("ECUE");

        JButtonEcue.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ecue" }));

        jLabel1.setText("Choississez les parametres puis valider !");

        JButtonReload.setText("Faire une nouvelle recherche");
        JButtonReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JButtonReloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JButtonDepartement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelDepartement))
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelAnnee)
                            .addComponent(JButtonAnnee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(JButtonSemestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(33, 33, 33)
                                .addComponent(JButtonUE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(44, 44, 44)
                                .addComponent(JButtonEcue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelSemestre)
                                .addGap(83, 83, 83)
                                .addComponent(jLabelUe)
                                .addGap(111, 111, 111)
                                .addComponent(jLabelEcue))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(241, 241, 241)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JButtonReload)
                            .addComponent(jLabel1))))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDepartement)
                    .addComponent(jLabelAnnee)
                    .addComponent(jLabelUe)
                    .addComponent(jLabelSemestre)
                    .addComponent(jLabelEcue))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JButtonDepartement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JButtonAnnee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JButtonUE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JButtonSemestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JButtonEcue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addComponent(JButtonReload)
                .addGap(35, 35, 35)
                .addComponent(jLabel1)
                .addGap(150, 150, 150))
        );
    }// </editor-fold>

    private void JButtonDepartementActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
    }                                                  

    private void JButtonSemestreActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
    }                                               

    private void JButtonReloadActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }



    /**
     * declaration du panel
     */
    private javax.swing.JComboBox JButtonAnnee;
    private javax.swing.JComboBox JButtonDepartement;
    private javax.swing.JComboBox JButtonEcue;
    private javax.swing.JComboBox JButtonSemestre;
    private javax.swing.JComboBox JButtonUE;
    private javax.swing.JButton JButtonReload;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelAnnee;
    private javax.swing.JLabel jLabelDepartement;
    private javax.swing.JLabel jLabelEcue;
    private javax.swing.JLabel jLabelSemestre;
    private javax.swing.JLabel jLabelUe;
    private ArrayList<String> ensDep;
    private ArrayList<String> ensAnnee;
    private ArrayList<String> ensSemestre;
    private ArrayList<String> ensUe;
    private ArrayList<String> ensEcue;
    private GUINotation GuiNotation;
    JOptionPane popup = new JOptionPane();
    
    
    
    // End of variables declaration
    
    
	/**
	 * action performed
	 */
	public void actionPerformed(ActionEvent e) {
	try{
		if ( e.getSource()== JButtonReload)
		{
			init();				
		}
		try{
			
		
			if (e.getSource()== JButtonDepartement){
				ensAnnee=Facade.getInstance().getNomsEnsAnnees(this.JButtonDepartement.getSelectedItem().toString());
				this.JButtonAnnee.removeAllItems();
				this.JButtonAnnee.addItem("Selection");
				for (int i=0 ; i < ensAnnee.size(); i++ ) {
			           this.JButtonAnnee.addItem(this.ensAnnee.get(i).toString());
	
			     }
				
				this.JButtonAnnee.addActionListener(this);	
			}
		}
		catch(NullPointerException ex){
			popup.showMessageDialog(null, "Veuillez faire une nouvelle recherche",
					"Erreur chargement", JOptionPane.ERROR_MESSAGE);
		}
		try{		
					
			if (e.getSource()== JButtonAnnee){
				ensSemestre=Facade.getInstance().getNomsEnsSem(this.JButtonDepartement.getSelectedItem().toString(),this.JButtonAnnee.getSelectedItem().toString());
				this.JButtonSemestre.removeAllItems();
				this.JButtonSemestre.addItem("Selection");
				for (int i=0 ; i < ensSemestre.size(); i++ ) {
				      this.JButtonSemestre.addItem(this.ensSemestre.get(i).toString());
				}		
			        this.JButtonSemestre.addActionListener(this);
			}
		}
		catch(NullPointerException ex){
			popup.showMessageDialog(null, "Veuillez faire une nouvelle recherche",
					"Erreur chargement", JOptionPane.ERROR_MESSAGE);
		}
		try{	
			if (e.getSource()== JButtonSemestre){
				ensUe=Facade.getInstance().getNomsEnsUE(this.JButtonDepartement.getSelectedItem().toString(), this.JButtonAnnee.getSelectedItem().toString(),this.JButtonSemestre.getSelectedItem().toString());	
				this.JButtonUE.removeAllItems();
				this.JButtonUE.addItem("Selection");
				for (int i=0 ; i < ensUe.size(); i++ ) {
				this.JButtonUE.addItem(this.ensUe.get(i).toString());
				}		
				this.JButtonUE.addActionListener(this);
	
			}
		}
		catch(NullPointerException ex){
			popup.showMessageDialog(null, "Veuillez faire une nouvelle recherche",
					"Erreur chargement", JOptionPane.ERROR_MESSAGE);
		}
		try{				
			if (e.getSource()== JButtonUE){
				ensEcue=Facade.getInstance().getNomsEnsEcue(this.JButtonDepartement.getSelectedItem().toString(), this.JButtonAnnee.getSelectedItem().toString(),this.JButtonSemestre.getSelectedItem().toString(),this.JButtonUE.getSelectedItem().toString());	
				this.JButtonEcue.removeAllItems();
				this.JButtonEcue.addItem("Selection");
				for (int i=0 ; i < ensEcue.size(); i++ ) {
				//	System.out.println(this.ensEcue.get(i).toString());
				this.JButtonEcue.addItem(this.ensEcue.get(i).toString());
				}	
				this.JButtonEcue.addActionListener(this);
			}
		}
			catch(NullPointerException ex){
				popup.showMessageDialog(null, "Veuillez faire une nouvelle recherche",
						"Erreur chargement", JOptionPane.ERROR_MESSAGE);
			}
		try{
			if(e.getSource()==JButtonEcue){
				this.guiNotation.setDataJcombo(this.JButtonEcue.getSelectedItem().toString());
				String temp;
				temp= Facade.getInstance().getCodeMatiereEnsEcue(this.JButtonEcue.getSelectedItem().toString() );
				this.guiNotation.setDataEcue(this.JButtonEcue.getSelectedItem().toString());
				this.guiNotation.setcodeEcue(temp);
				this.guiNotation.setNomAnnee(this.JButtonAnnee.getSelectedItem().toString());
				this.guiNotation.setNomDept(this.JButtonDepartement.getSelectedItem().toString());
			}
		}
			catch(NullPointerException ex){
				popup.showMessageDialog(null, "Veuillez faire une nouvelle recherche",
						"Erreur chargement", JOptionPane.ERROR_MESSAGE);
			}
	}
	catch(IndexOutOfBoundsException ex){
		System.out.println("erreur");
	}
			
		}
	
	/**
	 * reload les parametres du panel
	 */
	public void init(){
		this.JButtonDepartement.removeActionListener(this);
		this.JButtonAnnee.removeActionListener(this);			
		this.JButtonSemestre.removeActionListener(this);
		this.JButtonUE.removeActionListener(this);
		this.JButtonEcue.removeActionListener(this);
		
		this.JButtonDepartement.removeAllItems();
		this.JButtonAnnee.removeAllItems();
		this.JButtonSemestre.removeAllItems();
		this.JButtonUE.removeAllItems();
		this.JButtonEcue.removeAllItems();
		
		this.JButtonDepartement.addItem("Selection");
		this.JButtonAnnee.addItem("Selection");
		this.JButtonSemestre.addItem("Selection");
		this.JButtonUE.addItem("Selection");
		this.JButtonEcue.addItem("Selection");
		this.JButtonDepartement.addActionListener(this);
		
			for (int i=0 ; i < ensDep.size(); i++ ) {
		           this.JButtonDepartement.addItem(this.ensDep.get(i).toString());
		        }
	}
	
}
